package com.vanke.vms.management.dao.daoImpl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.vanke.vms.common.domain.User;
import com.vanke.vms.management.dao.LoginDao;

public class LoginDaoImpl extends HibernateDaoSupport implements LoginDao {

	@Override
	public User getUser(String username) {
		List<User> userList = (List<User>) getHibernateTemplate().find(
				"from User as u " + " where u.username = " + "'" + username
						+ "'");
		if (userList.size() != 0)
			return userList.get(0);
		else
			return null;
	}

	public void setValidate(User user) {
		getHibernateTemplate().update(user);
	}

	public void setForgetPass(String username) {
		SessionFactory sessionfactory = getHibernateTemplate()
				.getSessionFactory();
		Session session = sessionfactory.openSession();
		String sql = "update vms.user set validate = 5 where username = :username";
		session.createSQLQuery(sql).setParameter("username", username).executeUpdate();
		session.close();
	}
}
